layui.define(['jquery','form'], function (exports) {
	"use strict";
	var $ = layui.jquery,form=layui.form,object = {
		c: {data:[]}, // 配置项
		f: function (c) { // 数据初始化
			var s=new Date().getTime(),t = this;
			t.c.id = c.id?c.id:'simple_table_choiceshop_allot';
			t.c.tableId = c.tableelem?c.tableelem:'table-'+s;
			//c.data = JSON.parse('[{"accountName":"费玉清","timeTypeName":"00:00~00:59","operatorName":"周杰伦"}]');
			t.c.data = c.data; // 数据
			
			/**自定义的字段**/
			 t.c.prices = c.prices;
			 t.c.numTotal = 0;
		},
        load:function () { // 加载控件
        	var h = ''
    			+ this.loadTable()
    		+ '';
            $('#'+this.c.id).html(h);
            this.listenBtn();
        },
        loadTable:function () { // 加载表格
            return '<table class="layui-table" style="width: 100%; margin:0;"><thead>'+this.loadTableOfThead()+'</thead><tbody>'+this.loadTableOfTbody()+'</tbody></table>';
        },
        loadTableOfThead:function () { // 加载表头
            var h = '<thead><tr>';
            h += '<td style="text-align:center;">商品编码</th>';
            h += '<td style="text-align:center;">属性</th>';
            h += '<td style="text-align:center;">仓库</th>';
            h += '<td style="text-align:center;">箱规尺寸(cm)</th>';
            h += '<td style="text-align:center;">箱规重量(kg)</th>';
            h += '<td style="text-align:center;">建议售价(当地币)</th>';
            h += '<td style="text-align:center;">单价</th>';
            h += '<td style="text-align:center;">库存</th>';
            h += '<td style="text-align:center;">单箱数量</th>';
            h += '<td style="text-align:center;">起订箱数</th>';
            h += '<td style="text-align:center;">调拨箱数</th>';
            h += '<td style="text-align:center;">调拨数量</th>';
            h += '<td style="text-align:center;">货期</th>';
            h += '</tr></thead>';
			return h;
        },
        loadTableOfTbody:function () { // 加载表tbody
        	var t = this;
        	var h = '<tbody>';
            for(var i=0;i<t.c.data.length;i++){
	            h += '<tr>';
	            h += '<td style="text-align:center;">' + t.c.data[i].code + '</td>';
	            h += '<td style="text-align:center;">' + t.c.data[i].attr + '</td>';
	            h += '<td style="text-align:center;">' + t.c.data[i].warehouse + '</td>';
	            h += '<td style="text-align:center;">' + t.c.data[i].size + '</td>';
	            h += '<td style="text-align:center;">' + t.c.data[i].weight + '</td>';
	            h += '<td style="text-align:center;">' + t.c.data[i].retailPrice + '</td>';
	            h += '<td style="text-align:center;">' + t.c.data[i].originPrice + '</td>';
	            h += '<td style="text-align:center;">' + t.c.data[i].stock + '</td>';
	            h += '<td style="text-align:center;">' + t.c.data[i].numPerBox + t.c.data[i].unitName + '</td>';
	            h += '<td style="text-align:center;">' + t.c.data[i].numBuyMin + '</td>';
	            h += '<td style="text-align:center;"><input type="number" style="width: 50px; height:27px; border: 0.5px solid #FF5722; text-align: center;" data-index="' + i + '" data-type="numBox" lay-filter="_input"  value="' + t.c.data[i].numBox + '" ></td>';
	            h += '<td style="text-align:center; color: #FF5722; font-weight: 900;">' + t.c.data[i].numAllot + '</td>';
	            h += '<td style="text-align:center; color: #FF5722; font-weight: 900;"><i class="layui-icon layui-icon-tips" style="font-size: 20px; color: #1E9FFF;" data-index="' + i + '" lay-filter="_show_period"></i></td>';
                h += '</tr>';
            }
            
            h += '</tbody>';
			return h;
        },
        listenBtn:function () { // 添加监听
            var t = this;
            
			// 监听各个输入框
			$('input[lay-filter="_input"]').on('change',function (e) {
				var index = $(this).attr('data-index');
				var key = $(this).attr('data-type');
				var value = $(this).val();
				
				if (value == '') {
					value == 0;
				}
				if (value * 1 < t.c.data[index]['numBuyMin'] && value != 0) {
					layer.msg('调拨箱数不能少于' + t.c.data[index]['numBuyMin']);
					t.reload(t.c);
					return;
				}
				
				t.c.data[index][key] = value;
				
				// 计算总数量
				var numTotal = 0;
	            for(var i=0;i<t.c.data.length;i++){
	            	numTotal = numTotal + t.c.data[i]['numPerBox'] * (t.c.data[i]['numBox'] == undefined ? 0 : t.c.data[i]['numBox']);
	            }
	            
	            // 判断总数量
	            var prices = t.c.prices;
	            for(var i = prices.length - 1; i >= 0; i--){
	            	// 总数量超过折扣数量，重新赋值单价
	            	if (numTotal >= prices[i]['num'] || (i == 0)) {
	    	            
	            		var totalPrice = 0; // 总金额
	            		var totalNum = 0; // 总数量
	            		for(var j=0;j<t.c.data.length;j++){
	    	            	t.c.data[j]['numAllot'] = (t.c.data[j]['numBox'] == undefined ? 0 : t.c.data[j]['numBox']) * t.c.data[j]['numPerBox'];
	    	            	totalPrice += t.c.data[j]['originPrice'] * (t.c.data[j]['numBox'] == undefined ? 0 : t.c.data[j]['numBox']) * t.c.data[j]['numPerBox'];
	    	            	totalNum += t.c.data[j]['numAllot'];
	    	            }
	            		$('#id_total_price').html(totalPrice);
	            		$('#id_total_num').html(totalNum);
	    	            break;
					}
	            }
				t.reload(t.c);
				
				return;
			});
            
			// 监听货期图标
			$('i[lay-filter="_show_period"]').on('click',function (e) {
				var index = $(this).attr('data-index');

				$.ajax({
					type: "POST",
					url: '/hr/choiceshop/getPeriodInfo',
					data: {guid: t.c.data[index].guid},
					success: function (res) {
						if (res.code == 0) {
							layer.open({
						        area: ['500px', '300px'],
						        fix: false,//不固定
						        offset: 'auto',
						        shade: 0.3,//遮罩
								type: 1,
						        title: '货期信息',
						        shadeClose: true,// 弹层外区域关闭
								content: `<p style="padding:10px; font-size: 18px;">` + res.data.textPeriod + `<br>` + res.data.textArriveTime + `</p>`
							});
						} else {
							layer.msg(res.msg);
						}
					}
				});
				return;
			});
            
            // 表单控件重新加载
        	form.render();
        },
        reloadTable:function () { // 重新加载表格
            $('#'+this.c.tableId).html(this.loadTable());
            this.listenBtn();
        },
        getData:function () {
            return this.c.data;
        },
        init:function (c) {this.f(c);this.load();return object;},
        reload:function (c) {c.data = this.c.data;$('#' + this.c.id).empty();return this.init(c);},
        reloadData:function () {
        	var t = this;
			$.ajax({
				type: "POST",
				url: '/hr/choiceshop/detailData',
				data: {pcode: t.c.pcode},
				success: function (res) {
					if (res.code == 0) {
						t.c.data = res.data;
						t.reload(t.c);
					} else {
						layer.msg('加载失败，请刷新页面');
					}
				}
			});
        },
        addRow:function (row) {
        	var c = this.c;
        	c.data.push(row);
        	$('#' + c.id).empty();
        	return this.reload(c);
        },
        addRows:function (rows) {
        	var c = this.c;
        	for (var i = 0; i < rows.length; i++) {
	        	c.data.push(rows[i]);
			}
        	$('#' + c.id).empty();
        	return this.reload(c);
        },
        editRow:function (index, row) {
        	var c = this.c;
			c.data[index] = row;
        	$('#' + c.id).empty();
        	return this.reload(c);
        },
        editRowOfAttr:function (index, row) {
        	var c = this.c;
        	for(var key in row){
        		c.data[index][key] = row[key]
        	}
        	$('#' + c.id).empty();
        	return this.reload(c);
        },
        reloadRows:function (rows) {
        	var c = this.c;
        	c.data = rows;
        	return this.reload(c);
        },
        isAbleAllot: function() {
            var t = this;
			
			// 计算总数量
			var numTotal = 0;
            for(var i=0;i<t.c.data.length;i++){
            	numTotal = numTotal + t.c.data[i]['numPerBox'] * (t.c.data[i]['numBox'] == undefined ? 0 : t.c.data[i]['numBox']);
            }
            
            // 判断总数量不能低于最低数量
            var prices = t.c.prices;
            if (numTotal < prices[0]['num']) {
				return false;
			} else {
				return true;
			}
        }
	};
	exports('simple_table_choiceshop_allot', object);
});